package JavaUtils.uitl;

import JavaUtils.CSUtils.CFile;
import JavaUtils.CSUtils.Log.CLog;

import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Properties;
import java.util.Scanner;

public class CCmd {
    static final String tag = "C_cmd";
    public static boolean c_isLinux = false;

    public static void S_Init() {
        Properties props = System.getProperties();
        String OsName = props.getProperty("os.name");
        System.out.println("操作系统的名称：____ " + OsName);
        System.out.println("操作系统的版本号：_____ " + props.getProperty("os.version"));
        if (OsName.contains("Windows")) {
            CCmd.c_isLinux = false;
        } else {
            CCmd.c_isLinux = true;
        }
    }

    public static void S_test(String[] args) throws IOException {

        System.out.println("开始执行..............11...............");
        S_Runtime("unzip app.ipa");


        System.out.println("开始执行..............22...............");
        S_Runtime("zip -r appUnfold.ipa Payload");


        System.out.println("开始执行..............33...............");
        //S_Runtime("fastlane sigh resign $(pwd)/appUnfold.ipa  --signing_identity   'iPhone Distribution: yangyang li (V7CL5M7BN8)'  -p  'embedded.mobileprovision'");
        S_Runtime(new String[]{"fastlane", "sigh", "resign", "$(pwd)/appUnfold.ipa", "--signing_identity", "iPhone Distribution: yangyang li (V7CL5M7BN8)", "-p", "embedded.mobileprovision"});

        System.out.println("开始执行..............44...............");


    }


    public static void S_Runtime(String commom) throws IOException {
        CLog.f("执行命令.....  %s", commom);
        Scanner input = null;
        try {
            Process process = Runtime.getRuntime().exec(commom);
            InputStream is = process.getInputStream();
            input = new Scanner(is);
            while (input.hasNextLine()) {
                String log = input.nextLine();
                //System.out.println("命令log。。。"+log);
                CLog.i(tag,log);
            }
        } finally {
            if (input != null) {
                input.close();
            }
        }
    }

    public static void S_Runtime(String[] commom) throws IOException {
        StringBuilder cma = new StringBuilder("执行命令s..... ");
        for (String ss : commom) {
            cma.append("  ").append(ss);
        }
        CLog.i(tag,cma.toString());
        Scanner input = null;
        Process process = Runtime.getRuntime().exec(commom);
        InputStream is = process.getInputStream();
        input = new Scanner(is);
        while (input.hasNextLine()) {
            String log = input.nextLine();
            //System.out.println("命令log。。。..."+log);
            CLog.i(tag,log);
        }
    }


    public static void S_delete(String path) throws IOException {
        if (CCmd.c_isLinux) {
            S_Runtime("rm -rf " + path);
        } else {
            CFile.S_delete(path);
        }
    }

    public static String S_GotoPath(String gotoPath) throws Exception {
        String savePath = null;
        if (CCmd.c_isLinux) {
            savePath = System.getProperty("user.dir") + "/cd_shell.sh";
            CLog.i(tag,"当前路径....." + gotoPath + "......." + savePath);
            String shell = "cd " + gotoPath + " \t\necho \"当前路径\" \r\npwd  \t\nls -la";
            CFile.S_Save(savePath, shell);
            CCmd.S_Runtime("chmod 777 cd_shell.sh");
            CCmd.S_Runtime("./cd_shell.sh");
            CCmd.S_delete("cd_shell.sh");
        } else {
            CCmd.S_delete("cd " + gotoPath);
        }
        return savePath;
    }

    //获取当前进程id
    public static int getProcessID() {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        CLog.i(tag,"当前进程........" + runtimeMXBean.getName());
        return Integer.parseInt(runtimeMXBean.getName().split("@")[0]);
    }
}
